import Router from "koa-router";
import fs from 'fs';
import allowFilesUpload from "../../middleware/mid/allowFilesUpload.js";
import {excel} from "../../common/utils/utils.js";

const router = Router()
/*前端参数file*/
router.post('/readExcel', allowFilesUpload(true), (ctx, next) => {
  let file = ctx.request.files.file;
  let data = excel.reader(file.filepath);
  fs.unlinkSync(file.filepath); // 删除文件
  ctx.body = data;
});
router.post('/downLoadExcel', (ctx, next) => {
  let data = [
    {name: 'name', data: [[1, 2, 3], [4, 5, 6]]},
    {name: '哈哈', data: [[7, 8, 9], ['哈哈', 11, 12]]}
  ]
  ctx.body = excel.downloader(ctx, data, 'test.xlsx');

  // fs.writeFileSync('test.xlsx', buffer);
});

export default router;